// JavaScript Document

	function addSectionTag() {
		
		var scripts = document.getElementsByTagName("script");
		var script = scripts[scripts.length - 1];

		for (var i = 0, len = scripts.length; i < len; i++) {
			  
			  var src = scripts[i].getAttribute("src").split("?");
			  var args = src[1];
			  
					var argv = args.split("&");

					for (var j = 0, argc = argv.length; j < argc; j++) {
					  	var pair = argv[j].split("=");
					  		if (j == 0) 
								var usr = pair[1];		
							else {
								var app = pair[1];
								break;									
							}
					}
					
			break; 
		}
			tag = '<form action="secc_create.php?user='+usr+'&app='+app+'" name="formGenerateSection" id="formGenerateSection" method="post"> <input type="text" name="sectionName" id="sectionName" style="width:215px;" placeholder="Nombre de la Secci&oacute;n"/>Listo, entonces acepta! <input type="image" src="../imagenes/submit_section.png" name="submitSectionName" id="submitSectionName" align="right"/> <br /> <br /> <br /> </form>';	
			$("#sectionInput").append(tag);
	}
	
	function addSectionAddingElement(){
		
		var scripts = document.getElementsByTagName("script");
		var script = scripts[scripts.length - 1];

		for (var i = 0, len = scripts.length; i < len; i++) {
			  
			  var src = scripts[i].getAttribute("src").split("?");
			  var args = src[1];
			  
					var argv = args.split("&");

					for (var j = 0, argc = argv.length; j < argc; j++) {
					  	var pair = argv[j].split("=");
					  		if (j == 0) 
								var usr = pair[1];		
							else if (j == 1) {
								var app = pair[1];									
							}
							else if (j == 2){
								var secc = pair[1];
								break;	
							}
					}
					
			break; 
		}
		
		tag = '<table width="620" border="0" align="center"><tr><td><span class="byline"><form action="menu_section.php?usr='+usr+'&app='+app+'&sec='+secc+'" name="formAddImageElement" id="formAddImageElement" method="post"> <input type="image" src="../imagenes/menuOption.png" width="300" height="141" id="menu_element" name="menu_element"></form></span></td><td><span class="byline"><form action="text_section.php?usr='+usr+'&app='+app+'&sec='+secc+'" name="formAddTextElement" id="formAddTextElement" method="post"><input type="image" src="../imagenes/textSectionIcon.png" width="346" height="151" id="text_element" name="text_element"></form></span></td></tr></table><form action="image_section.php?usr='+usr+'&app='+app+'&sec='+secc+'" name="formAddMenuElement" id="formAddMenuElement" method="post"><p class="byline" align="center"><input type="image" src="../imagenes/imageHeader.png" width="475" height="194" id="image_element" name="image_element"><br></p></form>';	
			$("#elementAdding").append(tag);		
	}
	
	function myCall(appId,usrId) {
		
		var a = jQuery("#count:hidden").val();
    	a++;
    	document.getElementById('count').value = a;
	 
		var request = $.ajax({
			url: "../controladores/comandos/secciones/secc_Ajaxsearch.php?app="+appId+"&usr="+usrId+"&count="+(a++)+"",
			type: "GET",            
			dataType: "html"
		});

		request.done(function(msg) {
			$("#optionsBox").append(msg);          
		});

		request.fail(function(jqXHR, textStatus) {
			alert( "Request failed: " + textStatus );
		});
	}
	
	function removeElement(index){
		$("#menuElement"+index+"").remove();
	}
	
	function removeCurrentElement(index){
				
		var result = confirm("Estas seguro de que quieres eliminar este elemento?");
		
			if (result == true){
			 	
					var a = jQuery("#currents:hidden").val();
					a--;
					document.getElementById('currents').value = a;
					
					var request = $.ajax({
						url: "../controladores/comandos/elementos/element_AjaxDelete.php?id="+index+"&element=ME",
						type: "GET",            
						dataType: "html"
					});
			
					request.done(function(msg) {
						//$("#optionsBox").append(msg);          
					});
			
					request.fail(function(jqXHR, textStatus) {
						//alert( "Request failed: " + textStatus );
					});
					
					
					$("#currentMenuElement"+index+"").remove();	 	
			}
	}
	
	function removeImageElement(index){
		
		var result = confirm("Estas seguro de que quieres eliminar este elemento?");
		
		if (result == true){
			 						
					var request = $.ajax({
						url: "../controladores/comandos/elementos/element_AjaxDelete.php?id="+index+"&element=IE",
						type: "GET",            
						dataType: "html"
					});
			
					request.done(function(msg) {
						//$("#optionsBox").append(msg);          
					});
			
					request.fail(function(jqXHR, textStatus) {
						//alert( "Request failed: " + textStatus );
					});
					
				$("#image"+index+"").remove();
				$("#imageDeleteButton"+index+"").remove();
		

			}
			
	}
	
	function redirectTextEditCall(id){
		
		var scripts = document.getElementsByTagName("script");
		var script = scripts[scripts.length - 1];

		for (var i = 0, len = scripts.length; i < len; i++) {
			  
			  var src = scripts[i].getAttribute("src").split("?");
			  var args = src[1];
			  
					var argv = args.split("&");

					for (var j = 0, argc = argv.length; j < argc; j++) {
					  	var pair = argv[j].split("=");
					  		if (j == 0) 
								var usr = pair[1];		
							else if (j == 1) {
								var app = pair[1];									
							}
							else if (j == 2){
								var secc = pair[1];
								break;	
							}
					}
					
			break; 
		}
				
		window.location = "../vistas/text_section_edit.php?usr="+usr+"&sec="+secc+"&app="+app+"&elm="+id;	
	}
	
	function redirectMenuListCall(id){
		
		var scripts = document.getElementsByTagName("script");
		var script = scripts[scripts.length - 1];

		for (var i = 0, len = scripts.length; i < len; i++) {
			  
			  var src = scripts[i].getAttribute("src").split("?");
			  var args = src[1];
			  
					var argv = args.split("&");

					for (var j = 0, argc = argv.length; j < argc; j++) {
					  	var pair = argv[j].split("=");
					  		if (j == 0) 
								var usr = pair[1];		
							else if (j == 1) {
								var app = pair[1];									
							}
							else if (j == 2){
								var secc = pair[1];
								break;	
							}
					}
					
			break; 
		}
		
		window.location = "../vistas/menu_section_edit.php?usr="+usr+"&sec="+secc+"&app="+app+"&elm="+id;

	}
	
	function redirectGalleryElementCall(id){
		
		var scripts = document.getElementsByTagName("script");
		var script = scripts[scripts.length - 1];

		for (var i = 0, len = scripts.length; i < len; i++) {
			  
			  var src = scripts[i].getAttribute("src").split("?");
			  var args = src[1];
			  
					var argv = args.split("&");

					for (var j = 0, argc = argv.length; j < argc; j++) {
					  	var pair = argv[j].split("=");
					  		if (j == 0) 
								var usr = pair[1];		
							else if (j == 1) {
								var app = pair[1];									
							}
							else if (j == 2){
								var secc = pair[1];
								break;	
							}
					}
					
			break; 
		}
		
		window.location = "../vistas/image_section_edit.php?usr="+usr+"&sec="+secc+"&app="+app+"&elm="+id;
	}
	
	function deleteGalleryElement(id){		
	
		var result = confirm("Estas seguro de que quieres eliminar este elemento?");
			
			if (result == true) {
				var request = $.ajax({
				url: "../controladores/comandos/elementos/element_AjaxDelete.php?id="+id+"&element=GE",
				type: "GET",            
				dataType: "html"
			});
	
			request.done(function(msg) {		
				alert("El elemento fue eliminado satisfactoriamente, la pagina refrescara por favor espere");
				 window.location.reload();
			});
	
			request.fail(function(jqXHR, textStatus) {
				//alert( "Request failed: " + textStatus );
			});
		}		
	}
	
	function deleteTextElement(id){	
	
		var result = confirm("Estas seguro de que quieres eliminar este elemento?");
		
		if (result == true) {
			var request = $.ajax({
				url: "../controladores/comandos/elementos/element_AjaxDelete.php?id="+id+"&element=TE",
				type: "GET",            
				dataType: "html"
			});
	
			request.done(function(msg) {
				alert("El elemento fue eliminado satisfactoriamente, la pagina refrescara por favor espere");
				 window.location.reload();
			});
	
			request.fail(function(jqXHR, textStatus) {
				//alert( "Request failed: " + textStatus );
			});
			
		}		
	}
	
	function deleteMenuListElement(id){
		
		var result = confirm("Estas seguro de que quieres eliminar este elemento?");
		
		if (result == true) {
			var request = $.ajax({
				url: "../controladores/comandos/elementos/element_AjaxDelete.php?id="+id+"&element=ML",
				type: "GET",            
				dataType: "html"
			});
	
			request.done(function(msg) {
				alert("El elemento fue eliminado satisfactoriamente, la pagina refrescara por favor espere");
				 window.location.reload();
			});
	
			request.fail(function(jqXHR, textStatus) {
				//alert( "Request failed: " + textStatus );
			});			
		}
	}
	
	function deleteSection(id){
	 
	 	var scripts = document.getElementsByTagName("script");
		var script = scripts[scripts.length - 1];

		for (var i = 0, len = scripts.length; i < len; i++) {
			  
			  var src = scripts[i].getAttribute("src").split("?");
			  var args = src[1];
			  
					var argv = args.split("&");

					for (var j = 0, argc = argv.length; j < argc; j++) {
					  	var pair = argv[j].split("=");
					  		if (j == 0) 
								var usr = pair[1];		
							else if (j == 1) {
								var app = pair[1];									
							}
							else if (j == 2){
								var secc = pair[1];
								break;	
							}
					}
					
			break; 
		}
		 
	  var result = confirm("Estas seguro de que quieres eliminar esta seccion?");
	  
	  if (result == true) {
			var request = $.ajax({
				url: "../controladores/comandos/secciones/secc_AjaxDelete.php?id="+id,
				type: "GET",            
				dataType: "html"
			});
	
			request.done(function(msg) {
				location.href = "../vistas/sections_index.php?user_id="+usr+"&app_id="+app;
			});
	
			request.fail(function(jqXHR, textStatus) {
				//alert( "Request failed: " + textStatus );
			});
	  }
		
	}
	
	function deleteApplication(id){
	  
	  var result = confirm("Estas seguro de que quieres eliminar esta aplicacion?");
	  
		  if (result == true) {
				var request = $.ajax({
					url: "../controladores/comandos/aplicaciones/app_AjaxDelete.php?id="+id,
					type: "GET",            
					dataType: "html"
				});
		
				request.done(function(msg) {
					location.href = "../vistas/user_index.php?user_id="+msg;
				});
		
				request.fail(function(jqXHR, textStatus) {
					//alert( "Request failed: " + textStatus );
				});
		  }
	}